define([
        'app/utils/WebUtils',
        'app/utils/WeixinUtils',

        'text!app/tpl/top2Tabs.html',
        'text!app/tpl/orgManage.html',
        'text!app/tpl/me.html',
        'text!app/tpl/orgSetting.html',
        

        'app/directive/ManageOrganizationDirective',

        "app/service/RoomService",
        "app/service/OrganizationService"
        
        
        ], function (
        WebUtils,
        WeixinUtils,

        top2TabsTpl,
        orgManageTpl,
        meTpl,
        orgSettingTpl,

        ManageOrganizationDirective,

        RoomService,
        OrganizationService
    ) {

    

    var controller = function (orgUuid) {
        
        //0. Weixin Disabled
        WeixinUtils.disableAll();

        //1. Data Initialized
        OrganizationService.getOrganizationDetails(orgUuid,function(resp){

            if($.isNull(resp)){
                $.redirectTo($.wjtBaseBackend + "/mvc/authorize?state=login");
                return;
            }

            if(resp.status!="200"){
                $.redirectTo($.wjtBaseBackend + "/mvc/authorize?state=login");
                return;
            }

            console.log(resp);

            //2. Weixin Initialized
            WeixinUtils.disableAll();


            //3. tpl Initialized - Never change!!!
            var tpl = WebUtils.tplInitialized();

            
            //4. UI Initialized
            var top2tabsUI = _.template(top2TabsTpl)({
                "tab1_name": "微讲堂管理",
                "tab2_name": "个人中心"
            });

            var orgManageUI = _.template(orgManageTpl)({
                "attends":resp.result.attends,
                "logourl": resp.result.logourl,
                "name": resp.result.name,
                "uuid": resp.result.uuid,
                "rooms": resp.result.rooms,
                "unionid": $.getHashUrlParam("unionid")
            });

            var meUI = _.template(meTpl)({
               // "recommendOrgList": recommendOrgList
            });
            
            var orgSettingUI = _.template(orgSettingTpl)({
                "uuid": resp.result.uuid
            });
           

            //4. Assemble & Render
            tpl.append(top2tabsUI)
            tpl.find(".tab1-content").append(orgManageUI);
            tpl.find(".tab2-content").append(meUI);
            tpl.find(".additional-content").append(orgSettingUI);
            
            

            //5. Render - Never change!!!
            WebUtils.render(tpl);
     
            //6. Directive initialized;
            ManageOrganizationDirective.directive_redirectToCreateRoom();
                    
        });



        //7. routechange
        controller.onRouteChange = WebUtils.routeChange;

    };

    return controller;


});
